import reqwest from 'reqwest';

function checkLogin(spec) {
  spec = spec || {};

  spec.errorCb = function() {
    window.location.href = 'https://sso.toutiao.com/login/';
  };

  _getUserInfo(spec);
}

function _getUserInfo(spec) {
  spec = spec || {};

  var successCb = spec.successCb || function() {};
  var errorCb = spec.errorCb || function() {};
  var checkUrl = spec.url || '/user/info/';

  reqwest({
    url: checkUrl,
    method: 'get',
    success: (res) => {
      let data = res || {};
      if (data.message == 'error') {
        errorCb();
        return;
      }
      successCb(data);
    },
    error: () => {
      errorCb();
    }
  });
}

export default checkLogin;



// WEBPACK FOOTER //
// ./~/byted-toutiao-pc-business-components/src/js/user.js